/*
 * Copyright (c)  2020,  DataPipeline
 * All rights reserved.
 *
 * Id:AttributeBuilderHolder.java   2020-07-18 17:06 wanglijun
 */
package cn.datacare.orm.annotation.builder;

import cn.datacare.orm.annotation.Builder;
import cn.datacare.orm.util.BeanUtils;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2020
 * </p>
 * <p>
 * Company/Department: DataPipeline
 * </p>
 *
 * @author wanglijun
 * @version 1.0
 * @since 2020-07-18 17:06
 */
public class AttributeBuilderHolder {

    static Map<Class, Object> propertyHandlerMap = new ConcurrentHashMap<>();
    Object instance;
    private Annotation annotation;

    public AttributeBuilderHolder(Annotation annotation, Builder builder) {
        this.annotation = annotation;
        this.instance = newInstance(builder.value());
    }

    public Object newInstance(Class propertyHandlerClz) {
        if (propertyHandlerMap.containsKey(propertyHandlerClz)) {
            return propertyHandlerMap.get(propertyHandlerClz);
        }
        Object propertyHandler = BeanUtils.newInstance(propertyHandlerClz);
        propertyHandlerMap.put(propertyHandlerClz, propertyHandler);
        return propertyHandler;
    }


    public Annotation getAnnotation() {
        return annotation;
    }

    public Object getInstance() {
        return instance;
    }

    public boolean supportPersistGen() {
        return this.instance instanceof AttributePersistBuilder;
    }

    public boolean supportSelectMapping() {
        return this.instance instanceof AttributeSelectBuilder;
    }
}
